<?php
namespace Home\Controller;

/**
 * articleController
 * 文章
 */
class ArticleController extends CommonController {
    /**
     * 文章列表
     * @return
     */
    public function index() {
        $result = $this->getPagination('Article',['is_del'=>0]);
        foreach($result['data'] as $key => $val){
            $result['data'][$key]['novel_id'] = M('novel')->where(['id'=>$val['novel_id']])->getField('title');
        }
        $this->assign('list', $result['data']);
        $this->assign('rows_count', $result['total_rows']);
        $this->assign('page', $result['show']);
        $this->display();
    }

    /**
     * 添加文章页面
     */
    public function add(){
        $this->display();
    }

    /**
     * 添加文章操作
     */
    public function insert(){
        $article_Model = M('article');
        $data = I('post.data');
        if (!isset($data)) {
            return $this->errorReturn('无效的操作！');
        }

        if($data['title'] == '' || !$data['title']){
            return $this->errorReturn('文章标题不能为空！');
        }elseif($data['intro'] == '' || !$data['intro']){
            return $this->errorReturn('文章简介不能为空！');
        }elseif($data['novel_id'] == '' || !$data['novel_id']){
            return $this->errorReturn('请选择对应小说！');
        }

        //上传文章图片
        if($_FILES['pic']['tmp_name']){
            $info = $this->imgUpload('article_pic');
            if(!$info){
                return $this->errorReturn('文章图片上传失败请重试！');
            }else{
                $data['pic'] = '/Public/uploads/'.$info['pic']['savepath'].$info['pic']['savename'];
            }
        }

        $data['create_time'] = time();
        $data['update_time'] = time();
        $result = $article_Model->add($data);
        if (!$result) {
            return $this->errorReturn('添加失败，稍后请重试！');
        }

        return $this->successReturn('添加文章成功！', U('Article/index'));
    }


    /**
     * 编辑文章页面
     * @return
     */
    public function edit() {
        if (!isset($_GET['id'])) {
            return $this->error('参数错误，稍后请重试！');
        }

        //文章详情
        $article = M('article')->getById($_GET['id']);
        $article['novel_name'] = M('novel')->where(['id'=>$article['novel_id']])->getField('title');

        $this->assign('article', $article);

        $this->assign('id',$_GET['id']);
        $this->display();
    }

    /**
     * 更新文章信息
     * @return
     */
    public function update() {
        $article_Model = M('article');
        $data = I('post.data');
        if(!isset($data)){
            return $this->errorReturn('无效的操作！');
        }

        if($data['title'] == '' || !$data['title']){
            return $this->errorReturn('文章名称不能为空！');
        }elseif($data['intro'] == '' || !$data['intro']){
            return $this->errorReturn('文章简介不能为空！');
        }

        $data['update_time'] = time();

        if($data['novel_id'] == 0 || $data['novel_id'] == ''){
            $data['novel_id'] = $article_Model->where(['id'=>$data['id']])->getField('novel_id');
        }
        //上传封面
        if($_FILES['pic']['tmp_name']){
            $info = $this->imgUpload('article_pic');
            if(!$info){
                return $this->errorReturn('封面上传失败请重试！');
            }else{
                $data['pic'] = '/Public/uploads/'.$info['pic']['savepath'].$info['pic']['savename'];
            }
        }

        $result = $article_Model->where(['id'=>$data['id']])->save($data);
        if ($result) {
            return $this->successReturn('更新文章信息成功！', U('Article/index'));
        }else{
            return $this->errorReturn('操作失败，请重试！');
        }
    }

    /**
     * 删除文章
     */
    public function del(){
        $article_Model = M('article');
        $id = I('post.id');
        if(!$id){
            $this->ajaxReturn(['code'=>-1,'msg'=>'参数错误，请重试']);
        }

        $result = $article_Model->where(['id'=>$id])->setField('is_del',1);
        if($result){
            $this->ajaxReturn(['code'=>0,'msg'=>'删除成功!']);
        }else{
            $this->ajaxReturn(['code'=>-1,'msg'=>'删除失败，请重试!']);
        }
    }


}
